#include <bits/stdc++.h>

using namespace std;

class Solution {
    public:
        int lengthOfLongestSubsequence(vector<int> &nums, int target) {
            vector<int> f(target + 1, INT_MIN);
            f[0] = 0;
            int s = 0;
            for (int x : nums) {
                s = min(s + x, target);
                for (int j = s; j >= x; j--) {
                    f[j] = max(f[j], f[j - x] + 1);
                }
            }
            return f[target] > 0 ? f[target] : -1;
        }
    };
